package site.kexing.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import site.kexing.config.MybaitsUtils;
import site.kexing.mapper.ProductMapper;
import site.kexing.pojo.Product;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@Controller
public class ProductController {
    private static  final Logger logger = LoggerFactory.getLogger(ProductController.class);
    @Autowired
    private ProductMapper productMapper;

    //查询所有产品
    @RequestMapping("/selectallproducts")
    public String selectAllProducts(@RequestParam(defaultValue = "1") Integer currentPage,Model model){
        PageHelper.startPage(currentPage, 7);
        List<Product> products = productMapper.selectAllProducts();
        PageInfo<Product> pageInfo = new PageInfo<Product>(products,7);
        model.addAttribute("pageInfo",pageInfo);
        List<Product> list = pageInfo.getList();
        for (Product product:list){
            System.out.println(product);
        }
        return "product";
    }

    //根据id查询信息
    @RequestMapping("/selectproductbyid")
    public String selectProductByID(Integer id, Model model){
        Product product = productMapper.selectProductByID(id);
        model.addAttribute("product",product);
        return "productUpdate";
    }

    @RequestMapping("/updateproduct")
    public String updateProduct(@ModelAttribute("product") Product product){
        product.setUpdate_time(new Date());
        int i = productMapper.updateProduct(product);
        System.out.println(i);
        return "redirect:/selectallproducts";
    }

    @RequestMapping("/deleteproductbyid")
    public String deleteProductByID(Integer id){
        int i = productMapper.deleteProduct(id);
        return "redirect:/selectallproducts";
    }

    @RequestMapping("/insertproduct")
    public String insertProduct(@RequestParam("product_img_urls") MultipartFile uploadFile, HttpServletRequest request, Product product) throws IOException {

        if(!uploadFile.isEmpty()){
            String path = ResourceUtils.getURL("classpath:").getPath()+"static/equipmentImg";
            String realPath = path.replace('/', '\\').substring(1,path.length());
            System.out.println(realPath);

            final String fileName = MybaitsUtils.getUUID()+uploadFile.getOriginalFilename();

            String contentType = uploadFile.getContentType();
            if("image/jpeg".equals(contentType) || "image/jpg".equals(contentType) ){
                File file = new File(realPath,fileName);
                uploadFile.transferTo(file);
                System.out.println("图片上传成功!");
                String filePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/equipmentImg/" + fileName;
                product.setProduct_img_url(filePath);
                product.setProduct_num(MybaitsUtils.getUUID());
                product.setCreate_time(new Date());
                product.setUpdate_time(new Date());
                productMapper.insertProduct(product);
                return "redirect:/selectallproducts";
            } else {
                System.out.println("上传失败！");
                return "redirect:/selectequipmentbyid";
            }
        } else {
            System.out.println("上传失败！");
            return "redirect:/selectequipmentbyid";
        }
    }

    @RequestMapping("/selectproductbyname")
    public String selectProductByName(@RequestParam(defaultValue = "1") Integer currentPage,String product_name,Model model){
        PageHelper.startPage(currentPage, 7);
        List<Product> products = productMapper.selectProductByName(product_name);
        PageInfo<Product> pageInfo = new PageInfo<Product>(products,7);
        model.addAttribute("pageInfo",pageInfo);
        return "product";
    }
}
